package com.ellison.zookeeperdemo.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @Classname ZookeeperDemo
 * @Description TODO
 * @Author xx
 * Date 2021/6/10 22:04
 * Version 1.0
 */
public class ZookeeperDemo {

    private static String connectStr = "119.3.228.104:2181";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {
        //建立连接本身就是一个异步过程
        //埋了一个代码
        ZooKeeper zooKeeper = new ZooKeeper(connectStr, 5000, new WatcherDemo(countDownLatch));
        countDownLatch.await();

        nodeCreate(zooKeeper,"jack", "/demo1");
//        nodeChange(zooKeeper, "/demo1", "xx2");
//        nodeDelete(zooKeeper,"/demo1");


    }

    private static void nodeDelete(ZooKeeper client, String path) {
        try {
            client.exists(path, true);
            client.delete(path, -1);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void nodeChange(ZooKeeper client, String path, String value) {
        try {
            client.exists(path, true);
            client.setData(path, value.getBytes(), -1);

            client.getData(path, true, new Stat());
            client.setData(path, value.getBytes(), -1);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void nodeCreate(ZooKeeper client, String data, String path) {
        try {
            client.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
